package com.applink.sell_proof;

import java.util.Observable;
import java.util.Observer;

import android.app.Activity;
import android.app.ProgressDialog;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;

import com.applink.sell_proof.api.DataHelper;
import com.applink.sell_proof.api.DataResult;
import com.applink.sell_proof.api.Methods;
import com.applink.sell_proof.util.Utility;

public class InvitationCode extends Activity implements OnClickListener,
		Observer {

	private ProgressDialog processDialog;
	private EditText edtInvitationCode = null;
	private Button btnInvitationCode = null;
	private Context mContext;
	private Intent intentInvitation = null;
	private String msgInvitationCode = "";

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		mContext = this;
		if (BuildConfig.DEBUG) {
			EMarketing.getInstance().setInvitationCode(true);
			intentInvitation = new Intent(mContext, Splash.class);
			startActivity(intentInvitation);
			finish();
		} else {
			if (EMarketing.getInstance().isInvitationCode()) {
				intentInvitation = new Intent(mContext, Splash.class);
				startActivity(intentInvitation);
				finish();
			} else {
				if (!EMarketing.isOnline()) {
					Utility.showDialogNetwork(this,
							android.R.drawable.ic_dialog_alert);
				} else {
					setContentView(R.layout.invitation_code);
					init();
				}
			}
		}

	}

	private void init() {
		processDialog = new ProgressDialog(this);

		if (edtInvitationCode == null) {
			edtInvitationCode = (EditText) findViewById(R.id.edt_invitation_code);
		}

		if (btnInvitationCode == null) {
			btnInvitationCode = (Button) findViewById(R.id.btn_invitation_code);
		}
		listener();
	}

	private void listener() {
		btnInvitationCode.setOnClickListener(this);
	}

	@Override
	protected void onResume() {
		super.onResume();
		DataHelper.getInstance().addObserver(this);
	}

	@Override
	public void onClick(View v) {
		switch (v.getId()) {
		case R.id.btn_invitation_code:
			msgInvitationCode = edtInvitationCode.getText().toString().trim();
			if (msgInvitationCode.length() == 0) {
				Utility.showAlertDialog(mContext,
						android.R.drawable.ic_dialog_alert, getResources()
								.getString(R.string.invitation_blank),
						"Attention");
			} else {
				Utility.showWaitingStatus(processDialog);
				DataHelper.getInstance().inivationCode(msgInvitationCode);
			}

			break;

		default:
			break;
		}

	}

	@Override
	public void update(Observable observable, Object data) {
		Utility.hideWaitingStatus(processDialog);
		DataResult result = (DataResult) data;
		if (result.getMethodName() == Methods.invitationCode) {
			if (result.getData().toString() != null) {
				String code = (result.getData().toString());
				if (code.compareTo("0") == 0) {
					Toast.makeText(mContext, "This Access code is invalid.",
							5000).show();
					finish();
				} else if (code.compareTo("1") == 0) {
					EMarketing.getInstance().setInvitationCode(true);
					EMarketing.getInstance().setAccessCode(msgInvitationCode);
					intentInvitation = new Intent(mContext, Splash.class);
					startActivity(intentInvitation);
					finish();
				} else if (code.compareTo("2") == 0) {
					Toast.makeText(
							mContext,
							"This Access code is already actived \n Please contact with our service.",
							5000).show();
					finish();
				} else {
					Toast.makeText(
							mContext,
							"This Access code is invalid. \n Please contact with our service.",
							5000).show();
					finish();
				}
			} else {
				Toast.makeText(
						mContext,
						"This Access code is invalid. \n Please contact with our service.",
						5000).show();
				finish();
			}

		}

	}
}
